package com.nana.petadventure.app
{
	import com.nana.framework.base.App;
	import com.nana.framework.dll.Dll;
	import com.nana.petadventure.ui.guide.LoadingTips;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class AppLoading extends EventDispatcher
	{
		public static const FRIEND_LIST:String = "friendList";
		public static const SCENE_DATA:String = "sceneData";
		
		private static var _instance:AppLoading;
		
		private var _direction:Boolean;
		private var _sceneLoadingLayer:Sprite;
		private var _timer:Timer;
		private var _loadingDisplayObject:DisplayObject;
		private var loadingTips:LoadingTips;
		private var _showing:Boolean = false;
		private var _dataStatus:Object;
		private var _dataComp:Boolean = false;
		
		public function AppLoading(){
			this._dataStatus = {
				friendList:false,
				sceneData:false
			}
			if (_instance == null){
				super();
				_instance = this;
			} else {
				throw (new Error("实例化单例类出错-AppLoading"));
			}
		}
		
		public static function getInstance():AppLoading{
			if (_instance == null){
				_instance = new AppLoading();
			}
			return _instance;
		}
		
		public function show(_arg1:Boolean=false):void{
			this._direction = _arg1;
			if (!this._sceneLoadingLayer){
				this._sceneLoadingLayer = new Sprite();
			};
			this._sceneLoadingLayer.graphics.clear();
			this._sceneLoadingLayer.graphics.beginFill(0, 0.5);
			this._sceneLoadingLayer.graphics.drawRect(0, 0, App.appStage.stageWidth, App.appStage.stageHeight);
			this._sceneLoadingLayer.graphics.endFill();
			AppView.getInstance().loadingLayer.addChild(this._sceneLoadingLayer);
			this._showing = true;
			if (this._timer == null){
				this._timer = new Timer(1);
				this._timer.addEventListener(TimerEvent.TIMER, this.onTimer);
			}
			this._timer.start();
		}
		
		private function onTimer(_arg1:Event):void{
			if (this._showing){
				if (this._loadingDisplayObject == null){
					this._loadingDisplayObject = (Dll.getInstance().getDisplayObjectByName("SceneLoading") as DisplayObject);
				}
				if (this.loadingTips == null){
					this.loadingTips = new LoadingTips();
				} else {
					this.loadingTips.randomTip();
				}
				if (this._direction){
					this._loadingDisplayObject.scaleX = -1;
				} else {
					this._loadingDisplayObject.scaleX = 1;
				}
				this._loadingDisplayObject.x = (this._sceneLoadingLayer.width / 2);
				this._loadingDisplayObject.y = (this._sceneLoadingLayer.height / 2);
				this.loadingTips.x = (this._loadingDisplayObject.x - (this.loadingTips.width / 2));
				this.loadingTips.y = (this._loadingDisplayObject.y + (this._loadingDisplayObject.height / 2));
				this._sceneLoadingLayer.addChild(this._loadingDisplayObject);
				this._sceneLoadingLayer.addChild(this.loadingTips);
				this._timer.stop();
				this._showing = false;
			}
		}
		
		public function hide():void{
			var loadingLayer:Sprite = AppView.getInstance().loadingLayer;
			if (this._sceneLoadingLayer != null){
				if (loadingLayer.contains(this._sceneLoadingLayer)){
					loadingLayer.removeChild(this._sceneLoadingLayer);
				}
			}
			if (this._loadingDisplayObject != null){
				if (loadingLayer.contains(this._loadingDisplayObject)){
					loadingLayer.removeChild(this._loadingDisplayObject);
				}
			}
			if (this.loadingTips != null){
				if (loadingLayer.contains(this.loadingTips)){
					loadingLayer.removeChild(this.loadingTips);
				}
			}
			this._showing = false;
			if (this._timer){
				this._timer.stop();
			}
		}
		
		public function get dataComp():Boolean{
			return this._dataComp;
		}
		
		public function setComp(type:String):void{
			if (this.dataComp){
				return;
			}
			this._dataStatus[type] = true;
			var isComp:Boolean;
			for each (var status:Boolean in this._dataStatus) {
				if (status == false){
					isComp = false;
				}
			}
			if (isComp){
				this._dataComp = true;
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
	}
}